<?php
namespace Wj\Encipher\Symmetric;
class Password {

    private $algo = PASSWORD_DEFAULT;
    private $options = [];
    public function __construct(string $algo = null, array $options = null) {
        if (!empty($algo)) {
            if ($this->checkalgo($algo)) {
                $this->algo = $algo;
            }
        }
    }
    public function checkalgo(string $algo): bool {
        $allow_arr = [PASSWORD_DEFAULT, PASSWORD_BCRYPT, PASSWORD_ARGON2I, PASSWORD_ARGON2ID, PASSWORD_BCRYPT];
        if (!in_array($algo, $allow_arr)) {
            throw new \Throwable("algo:" . $algo . " is not supported by password_hash");
        }
        return true;
    }
    /**
     * 加密
     *
     * @param  [type] $data
     * @param  [type] $key
     * @param  [type] $aliase
     * @return void
     * @author wj
     * @date 2024-11-09
     */
    public function encrypt(string $plaintext): string {
        return password_hash($plaintext, $this->algo, $this->options);
    }
    /**
     * 校验加密
     *
     * @param  string  $ciphertext
     * @param  string  $hash
     * @return boolean
     * @author wj
     * @date 2024-11-09
     */
    public function verify(string $ciphertext, string $hash): bool {
        return password_verify($ciphertext, $hash);
    }

}